package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_JIU_ZHOU_SILVER_BUY_BUFF_FROM_CROSS, desc = "银两鼓舞")
public class JiuZhouSilverBuyBuffCmdFromCross extends AbstractCommand {


    private String costNum;

    private JiuZhouProto.JiuZhouDeployDataMsg deployTemp;


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossJiuZhouProto.CrossJiuZhouSilverBuyBuffRespMsg reqMsg = CrossJiuZhouProto.CrossJiuZhouSilverBuyBuffRespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        if (reqMsg.hasCost()) {
            costNum = reqMsg.getCost();
        }
        if (reqMsg.hasDeploy()) {
            deployTemp = reqMsg.getDeploy();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        String cost = GameConfig.GAME_MONEY_SILVER + "=" + costNum;
        // 取消预扣除
        player.getModule(PreReduceModule.class).restorePreReduce(cost);
        if (ret == 0) {
            // 扣除道具
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(cost), eLogMoneyType.JiuZhou, eLogMoneyType.JiuZhouSilverBuyBuffCost);

            JiuZhouProto.JiuZhouSilverBuyBuffRespMsg.Builder respMsg = JiuZhouProto.JiuZhouSilverBuyBuffRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setDeploy(deployTemp);
            player.sendPacket(ClientProtocol.U_JIU_ZHOU_SILVER_BUY_BUFF, respMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            JiuZhouProto.JiuZhouSilverBuyBuffRespMsg.Builder respMsg = JiuZhouProto.JiuZhouSilverBuyBuffRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_JIU_ZHOU_SILVER_BUY_BUFF, respMsg);
        }
    }
}
